﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using System.Collections;
using BSCMFF.Core.Exceptions.Database;

namespace BSCMFF.Web.UserControls.EmployeeTabs
{
    public partial class EvaluationTab : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RadGrid1.Culture = System.Threading.Thread.CurrentThread.CurrentCulture;


            
            
        }

        protected void RadGrid1_DataBinding(object sender, EventArgs e)
        {
           //e.Cancel = true;
        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;

            int eee_ID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
            int evaluatorID = Int32.Parse((editedItem["Evaluator"].Controls[1] as RadComboBox).SelectedValue);

            BSCEntities context = new BSCEntities();
            Evaluation_Evaluator_Employee eees = (from eee in context.Evaluation_Evaluator_Employee
                                                  where eee.ID == eee_ID
                                                  select eee).SingleOrDefault();

            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            if (newValues.Count == 1)
            {
                eees.EmployeeReaction = newValues["EmployeeReaction"].ToString();

                try
                {
                    context.SaveChanges();

                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't update reaction: " + ex.Message);
                }
            }
            //e.Canceled = true;
            RadGrid1.Rebind();
        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            int employeerID = Convert.ToInt32(Request.QueryString["EmployeeID"]);

            BSCEntities context = new BSCEntities();
            //List<User> users = (from eee in context.Evaluation_Evaluator_Employee
            //                    where eee.Evaluation_FK == evalID && eee.Evaluated == true && eee.Evaluator_FK == evaluatorID
            //                    select eee.User).ToList();

            List<Evaluation_Evaluator_Employee> eees = (from eee in context.Evaluation_Evaluator_Employee.Include("User").Include("User1").Include("Evaluation_Period")
                                                        where eee.Employee_FK == employeerID// && eee.Evaluated == true && eee.Evaluator_FK == evaluatorID
                                                        select eee).ToList();
            RadGrid1.DataSource = eees;
            //RadGrid1.DataBind();
        }

        

        
    }
}